package com.kbt.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.kbt.activity.*;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.model.data.AddressBean;
import com.kbt.model.data.Goods;
import com.kbt.model.data.TeMaiAndKaiQiangBean;
import com.kbt.model.holder.NewGoodItemHolder;
import com.kbt.ui.AddCarPopWindow;
import com.kbt.util.data.StaticData;
import com.kbt.util.utils.ImageManager;
import com.kbt.util.utils.SetingImage;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;


public class TeMaiAndKaiQiangAdapter extends BaseAdapter {

    Activity context;
    List<TeMaiAndKaiQiangBean> list;

    public TeMaiAndKaiQiangAdapter(Activity _context, List<TeMaiAndKaiQiangBean> _list) {
        this.list = _list;
        this.context = _context;
    }

    @Override
    public int getCount() {

            return list.size();
    }

    @Override
    public Object getItem(int position) {
            return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    /**
     * 再次赋值
     *
     * @param mlist
     */
    public void setList(List<TeMaiAndKaiQiangBean> mlist) {
        list = mlist;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NewGoodItemHolder itemHolder;
        if(convertView == null){
            itemHolder = new NewGoodItemHolder();
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.te_mai_and_kai_qiang_item, null);
            itemHolder.setNewGoodImag((ImageView) convertView.findViewById(R.id.tM_KQImg));
            itemHolder.setCarImg((ImageView) convertView.findViewById(R.id.tM_KQAddCar));
            itemHolder.setNewGoodLab((TextView) convertView.findViewById(R.id.tM_KQNewPrice));
            itemHolder.setNewGoodTitle((TextView) convertView.findViewById(R.id.tM_KQTitle));
            itemHolder.setOldPrice((TextView) convertView.findViewById(R.id.GoodOldPrice));
            itemHolder.setShouwan((ImageView) convertView.findViewById(R.id.shouwan));
            itemHolder.setTextView_kaiqiang((TextView) convertView.findViewById(R.id.kaiqiang));
            convertView.setTag(itemHolder);
        }else{
            itemHolder = (NewGoodItemHolder)convertView.getTag();
        }
            final TeMaiAndKaiQiangBean bean = list.get(position);

            itemHolder.getOldPrice().setText("￥"+bean.getGoods_price());
            itemHolder.getOldPrice().getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            itemHolder.getNewGoodLab().setText("￥"+bean.getGoods_new_price());
            itemHolder.getNewGoodTitle().setText(bean.getGoods_title());

        String url = bean.getGoods_icon();

        int screenWidth = context.getWindowManager().getDefaultDisplay().getWidth()/2-10;
        ViewGroup.LayoutParams lp = itemHolder.getNewGoodImag().getLayoutParams();
        lp.width = screenWidth;

        String[] url_params = url.split("&");

        int imageWidth = 0;
        int imageHeight = 0;
        if(url_params.length == 3){
            imageWidth = Integer.parseInt(url_params[1].split("=")[1]);
            imageHeight = Integer.parseInt(url_params[2].split("=")[1]);
        }

        if(imageWidth>0 && imageHeight>0){
            lp.height = new Float(imageHeight*screenWidth/imageWidth).intValue();
        }else{
            lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        itemHolder.getNewGoodImag().setLayoutParams(lp);
        itemHolder.getNewGoodImag().setMaxWidth(screenWidth);
        itemHolder.getNewGoodImag().setMaxHeight(screenWidth * 10);
        ImageManager.imageLoader.displayImage(Constants.ServiceInterFace.imageURL + bean.getGoods_icon(), itemHolder.getNewGoodImag(), ImageManager.getOptions(
                    R.drawable.loadings, R.drawable.loadings));


        int width = context.getWindowManager().getDefaultDisplay().getWidth()/8;
        SetingImage.setImageHeightWidthTwo(width,url,itemHolder.getShouwan());
        if(Integer.parseInt(bean.getStore_counts())<=0){
            itemHolder.getCarImg().setVisibility(View.INVISIBLE);
            itemHolder.getShouwan().setBackgroundResource(R.drawable.shouwan);
            itemHolder.getShouwan().setVisibility(View.VISIBLE);
        }else{

            if(TeMaiKaiQiangActivity.tag!=null) {
                if (TeMaiKaiQiangActivity.tag.equals("kaiQiang")) {
                    itemHolder.getNewGoodLab().setTextColor(Color.rgb(20,168,106));
                    itemHolder.getCarImg().setVisibility(View.GONE);
//                    itemHolder.getShouwan().setBackgroundResource(R.drawable.kaiqiang);
//                    itemHolder.getShouwan().setVisibility(View.VISIBLE);
                    itemHolder.getTextView_kaiqiang().setVisibility(View.VISIBLE);
                }else{
                    itemHolder.getCarImg().setVisibility(View.VISIBLE);
                    itemHolder.getShouwan().setVisibility(View.GONE);
                    itemHolder.getTextView_kaiqiang().setVisibility(View.GONE);
                }
            }
        }


        itemHolder.getNewGoodImag().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, GoodInfoActivity.class);
                        intent.putExtra("tag", TeMaiKaiQiangActivity.tag);
                        intent.putExtra("shopId", bean.getGoods_id());
                        context.startActivity(intent);
                    }
                });
        itemHolder.getNewGoodTitle().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GoodInfoActivity.class);
                intent.putExtra("tag", TeMaiKaiQiangActivity.tag);
                intent.putExtra("shopId", bean.getGoods_id());
                context.startActivity(intent);
            }
        });
        itemHolder.getCarImg().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(BaseApplication.getInstance().isLogin()){

                    AddCarPopWindow menuWindow = new AddCarPopWindow(context,
                            StaticData.PopWindowFromGoodFl,bean.getGoods_id(),bean.getGoods_icon(),bean.getGoods_new_price(),bean.getStore_counts());
                    //显示窗口
                    menuWindow.showAtLocation(context.findViewById(R.id.shopScrollview),
                            Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
                }else{
                    Intent intent = new Intent(context,LoginActivity.class);
                    context.startActivity(intent);
//                    context.finish();
                }
            }
        });


        return convertView;
    }
}
